package com.thinkgem.jeesite.modules.brain.common;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.modules.brain.entity.BRankDic;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 为每个租户的rank创建工具类
 * 非单例 每个租户每个季度都会创建一个
 *     (即每次rank改变时都需要创建一个)
 */
public class RankHandler {
    public  RankHandler(List<BRankDic> dicList){

        this.dicList=dicList;
        init();
    }
    private  List<BRankDic> dicList;

    private Integer[] section;  //区间生成


    void init (){
        Integer[] star= (Integer[])( dicList.stream().map(x->x.getRankStar()).collect(Collectors.toList())).toArray();  //star数组
        List<Integer> l=new ArrayList(star.length*2);
        for(int i=0;i<star.length;i++){
            if(i==0){
                l.add(0);
                l.add(star[0]);
                continue;
            }
            if(i==star.length-1&&star[i]==0) //最后
                break;
            l.add(l.get(l.size()-1)+1);
            l.add(l.get(l.size()-1)+star[i]);
        }
        section=(Integer[])l.toArray();

    }

    //获取区间下标 从0开始
    int getRankIndex(int starTotal){

        int insertpointOrIndex=Arrays.binarySearch(section, starTotal);

        if(insertpointOrIndex==-1)  //starTotal<0
            return -1;
        if(insertpointOrIndex>0)   //index
            return insertpointOrIndex>>>1;
        else
            return (-insertpointOrIndex-1)>>>1; //insertPoint
    }

    //获取当前星级
    BRankDic getCurrBRankDic(int starTotal){
        return dicList.get(getRankIndex(starTotal));

    }
    //获取当前级数剩余星级
    int getLevelRemainStar(int starTotal){
        int index=getRankIndex(starTotal);
        if(index ==0)
            return  starTotal;
        else
            return  starTotal-section[index*2];

    }




    public static void main(String[] args) {
        int insertpointOrIndex=Arrays.binarySearch(new int[]{1, 2,3,4,5}, 6);;
        System.out.println((insertpointOrIndex));
        System.out.println(insertpointOrIndex>>>1);
        System.out.println((-insertpointOrIndex-1)/2);
    }
}
